interface Player {
  bankerCount: number,
  lftId: number,
  nickname: string,
  owner: boolean,
  point: number,
  seatIndex: number,
  suitDescribe: Array<{name: string, count: number}>,
  suitMap: any,
  suitPatterns: any,
  uid: string,
  winner: boolean
}

declare var window: any

class UserLabel extends eui.Component {
  private userGroup: eui.Group
  private labelBg: eui.Image
  private useridGroup: eui.Group
  private userContent: eui.Group
  private userScore: eui.Label
  private username: eui.Label
  private userid: eui.Label
  public player: Player
  constructor(player) {
    super()
    this.once(eui.UIEvent.COMPLETE, this.onComplete, this)
    this.player = player
    this.skinName = `<e:Skin class="JinhuaSkin" width="199" height="309" xmlns:e="http://ns.egret.com/eui" xmlns:w="http://ns.egret.com/wing">
      <e:Image verticalCenter="0" horizontalCenter="0" id="labelBg" width="199" height="309"></e:Image>
      <e:Group verticalCenter="0" horizontalCenter="0" id="userGroup" width="199" height="309">
        <e:Label id="username" text="" textColor="0xffffff" size="28" horizontalCenter="0" y="24" percentWidth="70" textAlign="center" height="30"></e:Label>
        <e:Group id="useridGroup" x="0" y="72" percentWidth="100" height="43">
          <e:Label id="userid" textColor="0xffffff" text="" size="24" height="43"></e:Label>
        </e:Group>
        <e:Group id="userContent" x="0" y="87" width="180" height="168">
        </e:Group>
        <e:Label id="userScore" x="0" bottom="15" percentWidth="100" textAlign="center" height="104"></e:Label>
      </e:Group>
    </e:Skin>`
  }

  scoreNumber(point): Array<string> {
    var pointSigned = point > 0 ? '+' + point : point
    var numbers = (pointSigned + '').split('')
    return numbers.map(num => {
      if (point >= 0) {
        return `${num}_png`
      }
      if (num === '-') {
        return '-_png'
      }
      return `1${num}_png`
    })
  }

  private onComplete(e: eui.UIEvent) {
    const player = this.player
    const bgSrc = player.point >= 0 ? 'light-di_jinhua_png' : 'default-di_jinhua_png'
    this.labelBg.source = bgSrc

    var hLayout:eui.HorizontalLayout = new eui.HorizontalLayout()
    hLayout.gap = 10
    hLayout.horizontalAlign = egret.HorizontalAlign.CENTER
    this.useridGroup.layout = hLayout   /// 水平布局

    var vLayout:eui.VerticalLayout = new eui.VerticalLayout()
    vLayout.gap = 10
    vLayout.paddingTop = 5
    vLayout.horizontalAlign = egret.HorizontalAlign.CENTER
    this.userContent.layout = vLayout //设置问垂直布局

    this.player.suitDescribe.forEach(element => {
      const label = new eui.Label(`${element.name}: ${element.count}`)
      label.size = 23
      this.userContent.addChild(label)
    })

    this.username.text = this.player.nickname
    this.userid.text = 'ID: ' + this.player.lftId
    
    if (this.player.winner) {
      const winner = new eui.Image('big-winner_jinhua_png')
      winner.right = -10
      winner.bottom = -10
      this.userGroup.addChild(winner)
    }
    if (this.player.owner) {
      const owner = new eui.Image('owner_jinhua_png')
      owner.left = -20
      owner.top = 0
      this.userGroup.addChild(owner)
    }

    this.userScore.stroke = 1
    if (this.player.point >= 0) {
      this.userScore.textColor = 0xfff600
      this.userScore.strokeColor = 0xfff600
      this.userScore.text = '+' + this.player.point
    } else {
      this.userScore.textColor = 0xff0600
      this.userScore.strokeColor = 0xff0600
      this.userScore.text = '' + this.player.point   
    }

    // if (this.userScore.text.length >= 5) {
    //   this.userScore.size = 44
    // } else {
    //   this.userScore.size = 57
    // }
    this.userScore.size = 44
    this.userGroup.addChild(this.userScore)

    if (this.$hasAddToStage) {
      window.eventDispatcher.dispatchEvent(new egret.Event('ok'))
    } else {
      this.once(eui.UIEvent.ADDED_TO_STAGE, function () {
        setTimeout(() => {
          window.eventDispatcher.dispatchEvent(new egret.Event('ok'))
        }, window.labelRenderTime)
      }, this)
    }
  }
}

export default UserLabel